package org.example.sys.ui.home

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import org.example.sys.databinding.RecyclerItemBinding
import org.example.sys.bean.Book

class BookAdapter(private val context: Context, val list: MutableList<Book>) :
    RecyclerView.Adapter<BookAdapter.ViewHolder>() {
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val book = list.get(position)
        holder.binding.also {
            book.apply {
                it.tvTitle.text = title
                it.tvAuthor.text = author
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            RecyclerItemBinding.inflate(
                LayoutInflater.from(context), parent, false
            )
        )
    }

    override fun getItemCount(): Int {
        return list.size
    }

    class ViewHolder(val binding: RecyclerItemBinding) :
        RecyclerView.ViewHolder(binding.root)
}